import modelExtend from 'dva-model-extend'
import api from 'api'
import { pageModel } from 'utils/model'
const { pathToRegexp } = require("path-to-regexp")
const { queryAdminOperationLogList, updateAdminOperationLog, createAdminOperationLog,deleteAdminOperationLog,queryAdminUserOptions } = api
/**
 *
   //相关api信息
 queryAdminOperationLogList: `GET ${prefix}/admin/adminOperationLog/list`,
 updateAdminOperationLog: `PUT ${prefix}/admin/adminOperationLog`,
 createAdminOperationLog: `POST ${prefix}/admin/adminOperationLog`,
 deleteAdminOperationLog: `DELETE ${prefix}/admin/adminOperationLog/:ids`,
 queryAdminOperationLogDetails: `GET ${prefix}/admin/adminOperationLog/:id`,
 *
 */
export default modelExtend(pageModel, {
  namespace: 'admin_operation_log',
  state: {
    optionValueLabel: {},    options: {
      adminUserId: [],

      method: {
           'DELETE': 'DELETE',
           'POST': 'POST',
           'GET': 'GET',
           'PUT': 'PUT',
      },
    },
    trees: {
    },
    keyValues: {
      adminUserId: {}
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (pathToRegexp('/admin_operation_log').exec(location.pathname)) {
          dispatch({
            type: 'query',
            payload: {
              pageSize: 20,
              page: 1,
              ...location.query,
            },
          })
          dispatch({
            type: 'queryAdminUserOptions',
            payload: {},
          })
        }
      })
    },
  },

  effects: {
    *query({ payload }, { call, put }) {
      const data = yield call(queryAdminOperationLogList, payload)
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.payload.list,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.payload.total,
            },
          },
        })
      } else {
        throw data
      }
    },
   *create({ payload }, { call, put }) {
      const data = yield call(createAdminOperationLog, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },

    *update({ payload }, { select, call, put }) {
      const data = yield call(updateAdminOperationLog, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },
    *delete({ payload }, { select, call, put }) {
      const data = yield call(deleteAdminOperationLog, {ids:payload})
      if (!data.success) {
        throw data
      }
    },
    *queryAdminUserOptions({ payload }, { call, put }) {
      const data = yield call(queryAdminUserOptions, payload)
      if (data.success) {
        yield put({
          type: 'queryOptions',
          payload: {
              adminUserId: data.payload
          },
        })
        let kvs = {}
        data.payload.forEach(item=>{
          kvs[item.id] = item.username;
        })
        yield put({
          type: 'updateState',
          payload: {
            keyValues: {
              adminUserId: kvs
            }
          }
        })
      } else {
        throw data
      }
    },  },
  reducers: {
    showModal(state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal(state) {
      return { ...state, modalVisible: false }
    },
  },
})
